package com.stb.server.dao;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;

import com.stb.shared.domain.User;

public class UserDAOImpl implements UserDAO {
	@Override
	public void save(User user) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(user);
		} finally {
			pm.close();
		}
	}

	@Override
	public User get(String email) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
	        return pm.getObjectById(User.class, email);
		} catch (JDOObjectNotFoundException e) {
			return null;
		} finally {
			pm.close();
		}
	}

	@Override
	public void activate(String email) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
	        User user = pm.getObjectById(User.class, email);
	        if (user.isActive()) {
	        	throw new IllegalArgumentException("Already active");
	        }
	        user.setActive(true);
		} finally {
			pm.close();
		}
	}
}
